package controllers
{
	import flash.events.NetStatusEvent;
	import flash.net.NetStream;
	
	import models.LocalSetting;
	import models.StratusConnection;
	import models.vo.UserVO;

	[Bindable]
	public class VideoController
	{
		public var inStreams:Object = {};
		public var outStream:NetStream;
		[DexterBinding]
		public var localSetting:LocalSetting;
		[DexterBinding]
		public var stratus:StratusConnection;
		[DexterEvent]
		public function stratusInfo(event:NetStatusEvent):void{
			switch(event.info.code){
				case "NetConnection.Connect.Success":
					publishStream();
//					inStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
//					inStream.client = this;
//					inStream.addEventListener(NetStatusEvent.NET_STATUS,stratusInfo);
					break;
				case "NetStream.Connect.Success":
					if(event.info.stream == outStream){
						outStream.attachCamera(localSetting.cam);
						outStream.attachAudio(localSetting.mic);
						outStream.publish(UserVO.self.id);
					}
					break;
			}
		}
		[DexterEvent]
		public function playStream(id:String):void{
			var inStream:NetStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
			inStreams[id] = inStream;
			inStream.play(id);
		}
		[DexterEvent]
		public function stopPlay(id:String = null):void{
			if(!id){
				for each(var inStream:NetStream in inStreams){
					inStream.close();
				}
				inStreams = {};
			}else{
				inStreams[id].close();
				delete inStreams[id];
			}
		}
		[DexterEvent]
		public function getStream(id:String):NetStream{
			return inStreams[id];
		}
		public function publishStream():void{
			if(outStream)outStream.close();
			outStream = new NetStream(stratus,stratus.groupSpecifier.groupspecWithAuthorizations());
			outStream.client = this;
			outStream.addEventListener(NetStatusEvent.NET_STATUS,stratusInfo);
		}
	}
}